package controllers

import java.io.File

import dao.{AccountDao, MissionDao, UserDao}
import javax.inject.Inject
import play.api.mvc.{AbstractController, ControllerComponents}
import tool.{FormTool, Tool}
import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by yz on 17/4/2020
 */
class MissionController @Inject()(cc: ControllerComponents, accountDao: AccountDao, missionDao: MissionDao) extends
  AbstractController(cc){

  def downloadResultFile = Action.async {
    implicit request =>
      val userId = Tool.getUserId
      val data = FormTool.missionFileForm.bindFromRequest().get
      val missionId = data.missionId
      missionDao.selectByMissionId(userId, missionId).map {
        mission =>
          val missionIdDir = Tool.getMissionIdDirById(missionId)
          val resultDir = new File(missionIdDir, "result")
          val file = new File(resultDir, data.fileName)
          Ok.sendFile(file).withHeaders(
            //            CACHE_CONTROL -> "max-age=3600",
            CONTENT_DISPOSITION -> s"attachment; filename=${
              file.getName
            }",
            CONTENT_TYPE -> "application/x-download"
          )
      }
  }

}
